package com.buka.order.controller;

import com.buka.entity.PageResult;
import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.order.service.OrderService;
import com.buka.pojo.Order;
import com.buka.utils.BaseExceptionHandler;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单管理
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单添加
     *
     * @param order
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Order order) {
        orderService.add(order);
        return new Result("添加订单成功");
    }

    /**
     * 订单删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Long id) {
        try {
            orderService.delete(id);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("删除订单失败");
        }
        return new Result("删除订单成功");
    }

    /**
     * 订单修改
     *
     * @param order
     * @return
     */
    @PostMapping("/update")
    public Result update(Order order) {
        try {
            orderService.update(order);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("修改订单失败");
        }
        return new Result("修改订单成功");
    }

    /**
     * 查询所有订单
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Order> all = null;
        try {
            all = orderService.findAll();
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("查询失败");
        }
        return new Result("查询成功", all);
    }

    /**
     * 分页查询所有订单
     *
     * @return
     */
    @GetMapping("/findAllPage")
    public Result findAllPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {

        PageResult pageResult = null;
        try {
            Page<Order> allPage = orderService.findAllPage(page, size);
            pageResult = new PageResult();
            pageResult.setRows(allPage.getResult());
            pageResult.setTotal(allPage.getTotal());
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("查询失败");
        }

        return new Result("查询成功", pageResult);
    }

    /**
     * 根绝id查询订单
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Long id) {
        Order order = null;
        try {
            order = orderService.findById(id);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("查询失败");
        }
        return new Result("查询成功", order);
    }

    /**
     * 条件查询订单
     *
     * @param map
     * @return
     */
    @PostMapping("/findList")
    public Result findList(@RequestBody Map map) {
        List<Order> all = null;
        try {
            all = orderService.findList(map);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("查询失败");
        }
        return new Result("查询成功", all);
    }

    /**
     * 分页条件查询订单
     *
     * @return
     */
    @PostMapping("/findListPage")
    public Result findListPage(@RequestBody Map map,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {

        PageResult pageResult = null;
        try {
            Page<Order> allPage = orderService.findListPage(map,page, size);
            pageResult = new PageResult();
            pageResult.setRows(allPage.getResult());
            pageResult.setTotal(allPage.getTotal());
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("查询失败");
        }

        return new Result("查询成功", pageResult);
    }

    /**
     * 购买多个
     * @param orders
     * @return
     */
    @PostMapping("/buyMore")
    public Result buyMore(@RequestBody List<Order> orders){
        try {
            orderService.buyMore(orders);
        } catch (Exception e) {
            return new BaseExceptionHandler<>().error("购买失败");
        }
        return new Result("购买成功");
    }
}